library(tidyverse)
library(DeclareDesign)
library(rdss)

set.seed(343)
fixed_pop <-
  fabricate(
    N = 12,
    X = rep(c(0, 1), each = N/2),
    U = rnorm(N, sd = 0.25),
    potential_outcomes(Y ~ 0.2*Z + X + U)
  )

ra_declaration <- declare_ra(N = 12)
permutation_matrix <- obtain_permutation_matrix(ra_declaration)
estimates_list <- list()

for(i in 1:ncol(permutation_matrix)){
  
  dat <- 
    fixed_pop %>% 
    mutate(
      Z = permutation_matrix[,i],
      Y = reveal_outcomes(Y ~ Z)
    )
  
  estimates_list[[i]] <-
    bind_rows(
      DIM = tidy(lm_robust(Y ~ Z, data = dat, se_type = "none")),
      balance = tidy(lm_robust(X ~ Z, data = dat, se_type = "none")),
      OLS = tidy(lm_robust(Y ~ Z + X, data = dat, se_type = "none")),
      .id = "estimator"
    ) %>% 
    filter(term == "Z") %>% 
    mutate(sim_ID = i)
  
}

diagnosis_18.4 <- bind_rows(estimates_list)

write_rds(diagnosis_18.4, file = "diagnosis_objects/diagnosis_18.4.rds")
